/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Mar 16, 2012  12:08:00 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.promis.common
 * File Name : CustomerCreditNoteServices.java
 * TODO
 */
package jpa.service.promis.common;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;

import org.apache.log4j.Logger;

import promis.utility.CommonService;

import jpa.entitys.common.CommonEntity;
import jpa.entitys.promis.common.CustomerCreditNote;
import jpa.entitys.promis.common.CustomerCreditNotePK;
import jpa.service.common.JPAService;

/**
 * @author chatree
 *
 */
public class CustomerCreditNoteServices extends JPAService {
	
	private static final Logger log = Logger.getLogger(CustomerCreditNoteServices.class);

	/**
	 * @param em
	 */
	public CustomerCreditNoteServices(EntityManager em) {
		super(em);
		// TODO Auto-generated constructor stub
	}

	public List<CustomerCreditNote> findAll() throws Exception 
	{
		return super.ConvertToYoureObject(super.findAll("CustomerCreditNote.findAll"));
	}
	
	public CustomerCreditNote findById(CustomerCreditNotePK id) throws Exception {		
		
		return super.ConvertToYoureObject(super.find(id, "CustomerCreditNote.findById"));				
	}	
	
	public CustomerCreditNote InsertedOrUpdated(CustomerCreditNote vlaue) throws Exception {
		CustomerCreditNote obj = vlaue;
		try{		
			if(CommonService.IsNull(obj.getLastUpdOn()))
			{					
				obj.setSyncCreatedby(this.getUser());
				obj.setSyncCreatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.inserted(obj);
			}else{				
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	public CustomerCreditNote Updated(CustomerCreditNote vlaue) throws Exception {
		CustomerCreditNote obj = vlaue;
		try{
			
			if(vlaue.getId() != null)
			{
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}				
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	public List<CustomerCreditNote> Inserted(List<CustomerCreditNote> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(CustomerCreditNote obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getLastUpdOn()))
				{
					obj.setSyncCreatedby(this.getUser());
					obj.setSyncCreatedon(this.getCurrent_date());
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					common_lst.add(obj);
				}
			}		
			
			if(CommonService.getSize(common_lst) > 0)
				super.inserted(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}
	
	public List<CustomerCreditNote> Updated(List<CustomerCreditNote> vlauelist) throws Exception {		
		
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();
			for(CustomerCreditNote obj : vlauelist)
			{
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				common_lst.add(obj);
			}
			
			if(CommonService.getSize(common_lst) > 0)
				super.updated(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}

}
